package com.joy.web.user;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joy.common.exception.JsonException;
import com.joy.common.exception.PageException;
import com.joy.common.model.ApiResponse;
import com.joy.common.constant.CommonCons;
import com.joy.common.enums.Status;
import com.joy.common.model.vo.UserInfoVO;
import com.joy.web.admin.service.IUserService;
import com.joy.web.base.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName: UserProFileController
 * @Description: 用户中心
 * @Author: yjzhao
 * @Create: 2019-09-25  11:31
 **/
@Slf4j
@Controller
@RequestMapping("/user/profile")
public class UserProfileController extends BaseController {

    @Resource
    IUserService userService;

    /**
     * 用户个人中心
     * @return
     */
    @GetMapping(value = { "", "/", "/index" })
    public String profile(String userId, ModelMap map) {
        try {
            //获取用户详情
            UserInfoVO userInfoVO = userService.findUserInfoByUserId(userId);

            //传至前端
            map.put("userInfoVO", userInfoVO);

            return "pages/user/profile/index";
        } catch (Exception e) {
            log.info("/admin/user/profile MyException:");
            e.printStackTrace();
            throw new PageException(Status.UNKNOWN_ERROR, "获取人员信息报错了");
        }
    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/update" })
    @ResponseBody
    public ApiResponse update(UserInfoVO userInfoVO) {
        try {
            //更新用户详情
            boolean upSucc = userService.updateUserBaseInfo(userInfoVO);
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/user/update MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "更新人员信息报错了");
        }

    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/updatePwd" })
    @ResponseBody
    public ApiResponse updatePwd(String newPwd, String newPwd2){
        try {
            //校验密码相等
            if (StrUtil.isEmpty(newPwd) || StrUtil.isEmpty(newPwd2)
                || !newPwd.equals(newPwd2)) {
                return ApiResponse.ofStatus(Status.USER_UPDATE_PWD_ERROR, false);
            }
            //更新用户详情
            if (!this.getUserId().isPresent()) {
                return ApiResponse.ofStatus(Status.UN_LOGIN_ERROR, false);
            }
            boolean upSucc = userService.updatePwd(this.getUserId().get(),newPwd);
            userService.removeUserAuthorization();
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/user/update MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "更新人员信息报错了");
        }

    }
}
